package com.nidu.demo.role.executor.query;

import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.cola.exception.BizException;
import com.nidu.demo.common.exception.ErrorCodeConstants;
import com.nidu.demo.role.convertor.RoleConvertor;
import com.nidu.demo.role.dto.RoleCO;
import com.nidu.demo.role.gateway.RoleGateway;
import com.nidu.demo.role.model.Role;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 角色单查询执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class RoleSingleQryExe {

    private final RoleGateway gateway;

    private final RoleConvertor convertor;

    public SingleResponse<RoleCO> execute(Long id) {
        // 1. 查询数据
        Role role = gateway.getById(id);
        if(role == null){
            throw new BizException(ErrorCodeConstants.ROLE_NOT_FOUND.getCode(), ErrorCodeConstants.ROLE_NOT_FOUND.getMessage());
        }
        // 2. 转换为DTO返回
        RoleCO roleCO = convertor.toClientObject(role);

        return SingleResponse.of(roleCO);
    }

}